﻿using System;
using System.IO;
using System.Text;
using System.Threading;
using GameFramework.common;
using GameFramework.configs;
using GameFramework.net;
using GameFramework.scripts;
using GameFramework.timer;
using GameFramework.UI;
using Resources.UI.NetTest;
using UnityEngine;
using UnityEngine.UIElements;

namespace Game
{
    public class GameMain:MonoSingleton<GameMain>
    {
        private Thread serverThread;
        private TcpServer _tcpServer;
        public UIDocument Document;
        private NetTestView _view;

        private void Start()
        {
            //加载配置文件
            LocalConfig.Instance.loadConfig("Assets/AdressableResource/local_config/local_config.yaml");
            //资源检查(远程更新等等)
            
            //==============加载各种脚本=============
            //1.纯单例脚本进行初始化
            //加载属性脚本
            PropertyCalScriptManager.Instance.loadScripts();
            //加载通用（各个功能的逻辑脚本）脚本
            //加载消息类型和消息处理脚本
            MsgManager.Instance.loadMsgTypes_MsgScripts_MsgSerializers();
            
            //2.Scriptable 单例脚本加载(一般是各种配置，这步也可能最先调用，也可能是懒加载)
            
            //2.MonoBehavior单例管理器类挂在
            gameObject.AddComponent<TimerManager>();
            TcpClient tcpClient = gameObject.AddComponent<TcpClient>();
            tcpClient.initPipeline(pipeLine =>
            {
                //TODO 添加各种初入socket的handler
            });


            //==============开始游戏================
            
            //加载UI
            _view = new NetTestView();
            _view.setRootElement(Document.rootVisualElement);
        }

        public void startTcpServer()
        {
            _tcpServer = new TcpServer();
            serverThread = new Thread(_tcpServer.Start);
            serverThread.IsBackground = true;
            serverThread.Start();
        }




        public void testWrite()
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(Encoding.UTF8.GetBytes("hello_world"));
            Debug.Log("读取前============");
            Debug.Log("position："+ms.Position);
            Debug.Log("length:"+ms.Length);
            ms.Seek(0, SeekOrigin.Begin);
            int readByte = ms.ReadByte();
            Debug.Log("读取后============");
            Debug.Log("读取："+Encoding.UTF8.GetString(new byte[]{(byte)readByte}));
            
            Debug.Log("position："+ms.Position);
            Debug.Log("length:"+ms.Length);
            byte[] temp = new byte[3];
            ms.Read(temp,0,temp.Length);
            Debug.Log("读取后2============");
            Debug.Log("读取："+Encoding.UTF8.GetString(temp));
            Debug.Log("position："+ms.Position);
            Debug.Log("length:"+ms.Length);
        }

        public void testRead()
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(Encoding.UTF8.GetBytes("hello"));
            ms.Seek(0, SeekOrigin.Begin);
            byte[] temp = new byte[4];
            ms.Read(temp);
            int result = 0;
            for (int i = 0; i < 4; i++)
            {
                Debug.Log(temp[i]);
                result += temp[i] << ((3 - i) * 8);
            }

            Debug.Log(result);
        }

        private void OnDestroy()
        {
            if (_tcpServer != null)
            {
                //停止接收和发送线程
                serverThread.Abort();
                NetManager.Instance.closeAllClient();
            }
        }
    }
    
    
}